<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-tabs v-model="activeName">
      <el-tab-pane label="人员基本信息" name="first">
        <el-form
          :model="dataForm"
          :rules="dataRule"
          ref="dataForm"
          @keyup.enter.native="dataFormSubmit()"
          label-width="120px"
        >
          <el-form-item label="姓名" prop="roleName">
            <el-input v-model="dataForm.roleName" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="dataForm.phone" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="身份证号" prop="idNum">
            <el-input v-model="dataForm.idNum" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="识别门禁卡" prop="menjk">
            <el-upload
              action="https://jsonplaceholder.typicode.com/posts/"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt />
            </el-dialog>
          </el-form-item>
        </el-form>
        <div class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </div>
      </el-tab-pane>
      <el-tab-pane label="权限管理" name="second">
        <div style="margin-top: 20px">
          <el-checkbox v-model="checked3" label="备选项1" border size="medium"></el-checkbox>
          <el-checkbox v-model="checked84" label="备选项2" border size="medium"></el-checkbox>
          <el-checkbox v-model="checked74" label="备选项2" border size="medium"></el-checkbox>
        </div>
        <div style="margin-top: 20px">
          <el-checkbox v-model="checked64" label="备选项2" border size="medium"></el-checkbox>
          <el-checkbox v-model="checked54" label="备选项2" border size="medium"></el-checkbox>
          <el-checkbox v-model="checked44" label="备选项2" border size="medium"></el-checkbox>
        </div>
        <div style="margin-top: 20px">
          <el-checkbox v-model="checked34" label="备选项2" border size="medium"></el-checkbox>
          <el-checkbox v-model="checked24" label="备选项2" border size="medium"></el-checkbox>
          <el-checkbox v-model="checked14" label="备选项2" border size="medium"></el-checkbox>
        </div>

        <div class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </div>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      activeName: "first",
      checked44: "",
      checked54: "",
      checked64: "",
      checked74: "",
      checked24: "",
      checked34: "",
      checked14: "",
      checked3: "",
      checked84: "",
      visible: false,
      posiList: [],
      menuList: [],
      menuListTreeProps: {
        label: "name",
        children: "children"
      },
      posiListTreeProps: {
        children: "children",
        label: "label"
      },
      dataForm: {
        id: 0,
        roleName: "",
        remark: ""
      },
      dataRule: {
        roleName: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
        idNum: [{ required: true, message: "身份证号不能为空", trigger: "blur" }],
        menjk: [{ required: true, message: "门禁卡不能为空", trigger: "blur" }]
      },
      dialogImageUrl: "",
      dialogVisible: false,
      tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl("/sys/org/list"),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.posiList = data.list;
      });
      this.$http({
        url: this.$http.adornUrl("/sys/menu/list"),
        method: "get",
        params: this.$http.adornParams()
      })
        .then(({ data }) => {
          this.menuList = treeDataTranslate(data, "menuId");
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/role/info/${this.dataForm.id}`),
              method: "get",
              params: this.$http.adornParams()
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.roleName = data.role.roleName;
                this.dataForm.remark = data.role.remark;
                this.dataForm.phone = data.role.phone;
                var idx = data.role.menuIdList.indexOf(this.tempKey);
                if (idx !== -1) {
                  data.role.menuIdList.splice(
                    idx,
                    data.role.menuIdList.length - idx
                  );
                }
              }
            });
          }
        });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/role/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              roleId: this.dataForm.id || undefined,
              roleName: this.dataForm.roleName,
              remark: this.dataForm.remark,
              menuIdList: [].concat()
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
<style   scoped>
.dialog-footer {
  text-align: right;
}
</style>
